package com.yonyou.crm.cum.customer.queryplan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.crm.cum.customer.entity.CustomerDO;
import com.yonyou.crm.sys.role.constant.RoleTypeConstant;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class CustomerQueryPlanHandle extends BaseBizServiceImpl<CustomerDO>{
	
	public List<String> handle(Map<String, Object> param){

		List<String> idList = new ArrayList<String>();
		Object searchPlanObj = param.get("id");
		
		String roleType = InvocationInfoProxy.getParameter(LoginContextConstant.ROLETYPE);

		if (searchPlanObj != null) {
			int searchPlan = Integer.parseInt(searchPlanObj.toString());
			//避免和列表条件冲突
			param.put("id", null);
			param.put("userId", InvocationInfoProxy.getParameter(LoginContextConstant.UserId));
			
			//销售员
			if (RoleTypeConstant.SALESPERSON.equals(roleType)) {
				
				if (searchPlan == 5) {//全部
					param.put("orderBy", "c.name asc");
				}
				else if (searchPlan == 6) {//我负责
					idList = getIdList("selectListByOwner", param);
					
				}else if (searchPlan == 7) {//我参与
					idList = getIdList("selectListByRelUser", param);
					
				}else if (searchPlan == 8) {//我关注
					idList = getIdList("selectListByFollow", param);
					
				}else if (searchPlan == 9) {//最近查看
					idList = getIdList("selectListByView", param);
					
				}else if (searchPlan == 10) {//一周前跟进
					param.put("followTimeAgo", "1 week");
					param.put("orderBy", "s.follow_time desc");
				}
			}
			//销售经理
			else if (RoleTypeConstant.SALESMANGER.equals(roleType)) {
				if (searchPlan == 11) {//全部
					param.put("orderBy", "c.name asc");
				}
				else if (searchPlan == 12) {//我负责
					idList = getIdList("selectListByOwner", param);
					
				}else if (searchPlan == 13) {//我参与
					idList = getIdList("selectListByRelUser", param);
					
				}else if (searchPlan == 14) {//我关注
					idList = getIdList("selectListByFollow", param);
					
				}else if (searchPlan == 15) {//最近查看
					idList = getIdList("selectListByView", param);
					
				}else if (searchPlan == 16) {//一周前跟进
					param.put("followTimeAgo", "1 week");
					param.put("orderBy", "s.follow_time desc");
				}
			}
			//公司内勤
			else if (RoleTypeConstant.COMPANYOFFICE.equals(roleType)) {
				if (searchPlan == 17) {//全部
					param.put("orderBy", "c.name asc");
				}
				else if (searchPlan == 18) {//我关注
					idList = getIdList("selectListByFollow", param);
					
				}else if (searchPlan == 19) {//最近查看
					idList = getIdList("selectListByView", param);
					
				}else if (searchPlan == 20) {//最近创建
					param.put("orderBy", "c.created_time desc");
					
				}else if (searchPlan == 21) {//一周前跟进
					param.put("followTimeAgo", "1 week");
					param.put("orderBy", "s.follow_time desc");
				}
				
				//公司客户分配
				if (searchPlan == 22) {//全部
					param.put("orderBy", "c.name asc");
				}
				else if (searchPlan == 23) {//未分配
					idList = getIdList("selectListByNoAssign", param);
					
				}else if (searchPlan == 24) {//已分配
					idList = getIdList("selectListByAssign", param);
					
				}else if (searchPlan == 25) {//最近查看
					idList = getIdList("selectListByView", param);
					
				}else if (searchPlan == 26) {//最近创建
					param.put("orderBy", "c.created_time desc");
					
				}
			}
			
			param.put("userId", null);
		}

		return idList;
	}
	
	private List<String> getIdList(String funcName, Map<String, Object> param) {
		List<String> idList = dao.selectStrList(CustomerDO.class, funcName, param);
		//拼接无数据条件
		if (idList == null || idList.size() == 0) {
			idList.add("0");
		}
		return idList;
	}
	
}
